package com.yuzhihao.writejvm.attributes.runtimevta;

import com.yuzhihao.writejvm.clazz.ClassInfo;
import com.yuzhihao.writejvm.read.Parse;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Wherever a type is used in a declaration or expression, the type_path structure identifies which part of the type is annotated
 * <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.20.2">TypePath</a>
 *
 * @author yzh
 * @since 2021年12月11日16:55:26
 */
@Data
@Log4j2
public class TypePath implements Parse {

    private int path_length_u1 = 1;

    private int path_length;

    private List<Path> paths = new ArrayList<>();

    @Override
    public void parse(DataInputStream dis, ClassInfo ci) {

        try {
            this.path_length = Parse.byteToInt(this.path_length_u1, dis);

            int i = 0;
            while (i < this.path_length){
                Path path = new Path();
                path.parse(dis,ci);
                this.paths.add(path);
                i++;
            }
        } catch (IOException e) {
            log.error(e);
        }

    }


    @Data
    @Log4j2
    public static class Path implements Parse{

        private int type_path_kind_u1;
        private int type_path_kind;

        private int type_argument_index_u1;
        private int type_argument_index;

        @Override
        public void parse(DataInputStream dis, ClassInfo ci) {
            try {
                this.type_path_kind = Parse.byteToInt(this.type_path_kind_u1, dis);
                this.type_argument_index = Parse.byteToInt(this.type_argument_index_u1, dis);
            } catch (IOException e) {
                log.error(e);
            }
        }
    }
}
